package org.gnyxy.service.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.gnyxy.constant.MessageConstant;
import org.gnyxy.entity.Dish;
import org.gnyxy.exception.BaseException;
import org.gnyxy.mapper.SearchMapper;
import org.gnyxy.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchMapper searchMapper;

    /**
     * 搜索预选
     * @param name 名称
     * @return Set
     */
    @Override
    public Set<String> getNameSearchName(String name) {
        if (StringUtils.isBlank(name)){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        Set<String> dishNames = searchMapper.getDishNameByName(name);
        Set<String> materialNames = searchMapper.getMaterialNameByName(name);
        Set<String> set = new HashSet<>(dishNames);
        set.addAll(materialNames);
        return set;
    }

    /**
     * 根据名称搜索
     * @param name
     * @return
     */
    @Override
    public Set<Dish> getDishByName(String name) {
        if (StringUtils.isBlank(name)){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        // 查询dish的名称
        Set<Dish> dishes = searchMapper.getDishByName(name);
        Set<Long> materialIds = searchMapper.getMaterialIdByName(name);
        Set<Long> dishIds = null;
        if (!CollectionUtils.isEmpty(materialIds)){
            dishIds = searchMapper.getDishIdsByMaterialIds(materialIds);
        }

        if (!CollectionUtils.isEmpty(dishIds)){
            dishes.addAll(searchMapper.getDishByDishIds(dishIds));
        }

        return dishes;
    }
}
